package com.person.util;

import org.apache.poi.xwpf.usermodel.*;

/**
 * Word文档字体样式工具类
 * 用于统一设置文档中的字体样式、大小等属性
 */
public class WordFontStyleUtil {

    //设置word文档中单元格的字体和字号
    public static void setCellStyle(XWPFTableCell cell, String context, String flag) {
        XWPFParagraph xwpfParagraph = cell.getParagraphs().get(0);
        if ("核查情况及立案理由".equals(flag) || "核查情况及不予立案理由".equals(flag) || "案源内容".equals(flag) || "消费者权益争议事实".equals(flag)) {
            xwpfParagraph.setIndentationFirstLine(520);
        }
        XWPFRun xwpfRun = xwpfParagraph.createRun();
        xwpfRun.setText(context);
        xwpfRun.setFontSize(12);
        xwpfRun.setFontFamily("仿宋_GB2312");
        if ("核查情况及立案理由".equals(flag) || "核查情况及不予立案理由".equals(flag)) {
            XWPFParagraph xwpfParagraph1 = cell.addParagraph();
            xwpfParagraph1.setAlignment(ParagraphAlignment.RIGHT);
            XWPFRun xwpfRun1 = xwpfParagraph1.createRun();
            xwpfRun1.setText("核查人员：      （执法证号：           ）");
            xwpfRun1.setFontSize(12);
            xwpfRun1.setFontFamily("仿宋_GB2312");
            XWPFParagraph xwpfParagraph2 = cell.addParagraph();
            xwpfParagraph2.setAlignment(ParagraphAlignment.RIGHT);
            XWPFRun xwpfRun2 = xwpfParagraph2.createRun();
            xwpfRun2.setText("（执法证号：           ）");
            xwpfRun2.setFontSize(12);
            xwpfRun2.setFontFamily("仿宋_GB2312");
            cell.addParagraph();
            XWPFParagraph xwpfParagraph3 = cell.addParagraph();
            xwpfParagraph3.setIndentationFirstLine(4800);
            XWPFRun xwpfRun3 = xwpfParagraph3.createRun();
            xwpfRun3.setText("年  月  日");
            xwpfRun3.setFontSize(12);
            xwpfRun3.setFontFamily("仿宋_GB2312");
        }
        if ("案源内容".equals(flag)) {
            cell.addParagraph();
            XWPFParagraph xwpfParagraph1 = cell.addParagraph();
            xwpfParagraph1.setIndentationFirstLine(21 * 240);
            XWPFRun xwpfRun1 = xwpfParagraph1.createRun();
            xwpfRun1.setText("登记人：");
            xwpfRun1.setFontSize(12);
            xwpfRun1.setFontFamily("仿宋_GB2312");
            XWPFParagraph xwpfParagraph2 = cell.addParagraph();
            xwpfParagraph2.setIndentationFirstLine(25 * 240);
            XWPFRun xwpfRun2 = xwpfParagraph2.createRun();
            xwpfRun2.setText("年    月    日");
            xwpfRun2.setFontSize(12);
            xwpfRun2.setFontFamily("仿宋_GB2312");
        }
    }

    /**
     * 设置标题样式
     *
     * @param paragraph 段落对象
     * @param text      文本内容
     */
    public static void setTitleStyle(XWPFParagraph paragraph, String text) {
        paragraph.setAlignment(ParagraphAlignment.CENTER);
        paragraph.setSpacingBefore(300);
        paragraph.setSpacingBetween(24, LineSpacingRule.EXACT);
        XWPFRun run = paragraph.createRun();
        run.setText(text);
        run.setFontSize(22);
        run.setBold(true);
        run.setFontFamily("方正小标宋简体");
    }

    /**
     * 设置副标题样式
     *
     * @param paragraph 段落对象
     * @param text      文本内容
     */
    public static void setSubTitleStyle(XWPFParagraph paragraph, String text) {
        paragraph.setAlignment(ParagraphAlignment.CENTER);
        paragraph.setSpacingBetween(24, LineSpacingRule.EXACT);
        XWPFRun run = paragraph.createRun();
        run.setText(text);
        run.setFontSize(16);
        run.setFontFamily("仿宋_GB2312");
    }

    /**
     * 设置正文样式
     *
     * @param paragraph       段落对象
     * @param text            文本内容
     * @param indentFirstLine 是否首行缩进
     */
    public static void setContentStyle(XWPFParagraph paragraph, String text, boolean indentFirstLine) {
        paragraph.setSpacingBetween(24, LineSpacingRule.EXACT);
        if (indentFirstLine) {
            paragraph.setIndentationFirstLine(700); // 首行缩进
        }
        XWPFRun run = paragraph.createRun();
        run.setText(text);
        run.setFontSize(16);
        run.setFontFamily("仿宋_GB2312");
    }

    /**
     * 设置署名样式
     *
     * @param paragraph 段落对象
     * @param text      文本内容
     */
    public static void setSignatureStyle(XWPFParagraph paragraph, String text) {

        if (text.contains("年")) {
            paragraph.setIndentationFirstLine(20 * 240);
        } else {
            paragraph.setAlignment(ParagraphAlignment.RIGHT);
        }
        paragraph.setSpacingBetween(24, LineSpacingRule.EXACT);
        XWPFRun run = paragraph.createRun();
        run.setText(text);
        run.setFontSize(16);
        run.setFontFamily("仿宋_GB2312");
    }

    /**
     * 添加换行
     *
     * @param run XWPFRun对象
     */
    public static void addLineBreak(XWPFRun run) {
        run.addBreak();
    }

    /**
     * 设置自定义样式
     *
     * @param paragraph       段落对象
     * @param text            文本内容
     * @param fontSize        字体大小
     * @param fontFamily      字体名称
     * @param bold            是否加粗
     * @param alignment       对齐方式
     * @param indentFirstLine 首行缩进量（0表示不缩进）
     */
    public static XWPFRun setCustomStyle(XWPFParagraph paragraph, String text, int fontSize,
                                         String fontFamily, boolean bold, ParagraphAlignment alignment,
                                         int indentFirstLine) {
        paragraph.setAlignment(alignment);
        if (indentFirstLine > 0) {
            paragraph.setIndentationFirstLine(indentFirstLine);
        }
        XWPFRun run = paragraph.createRun();
        run.setText(text);
        run.setFontSize(fontSize);
        run.setFontFamily(fontFamily);
        run.setBold(bold);
        return run;
    }
}